package com.cqupt.scanning.system.acl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.entity.dto.UserDTO;
import com.cqupt.scanning.system.acl.entity.vo.UserVO;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @Data: 2021/7/17 13:08
 * @Author: 宋宝梁
 */
@RestController
@RequestMapping("/admin/acl/user")
public class AclUserController {

    private AclUserService aclUserService;

    @Autowired
    public AclUserController(AclUserService aclUserService) {
        this.aclUserService = aclUserService;
    }

    /**
     * 获取所有账户的分页列表（支持用户名模糊查询）
     * @param query
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAllUsers")
    @PreAuthorize("hasAuthority('user.getAllUsers')")
    public R getAll(@RequestParam("username") String query,
                    @RequestParam(value = "pageNum", defaultValue = "1") Integer current,
                    @RequestParam(value = "pageSize", defaultValue = "10") Integer size) {
        Page<AclUser> page = new Page<>(current, size);
        return aclUserService.getAllAdmin(page, query);
    }

    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    @GetMapping("/getOneUser/{id}")
    @PreAuthorize("hasAuthority('user.getOneUser')")
    public R getOneUser(@PathVariable("id") @Min(value = 1, message = "id必须大于等于1") Integer userId) {
        return aclUserService.getOneUser(userId);
    }

    /**
     * 新增管理员账户（同时包含分配角色）
     * @return
     */
    @PostMapping("/addUser")
    @PreAuthorize("hasAuthority('user.addUser')")
    public R addUser(@RequestBody @Validated UserVO userVO) {
        return aclUserService.addUser(userVO);
    }

    /**
     * 根据id更新用户
     * @param userDTO
     * @return
     */
    @PutMapping("/updateUser/{roleId}")
    @PreAuthorize("hasAuthority('user.updateUser')")
    public R updateUser(@RequestBody @Validated UserDTO userDTO, @PathVariable Integer roleId) {
        return aclUserService.updateUser(userDTO, roleId);
    }

    /**
     * 根据id删除用户和角色-用户关系
     * @param userId
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAuthority('user.remove')")
    public R removeUserById(@PathVariable("id") @NotNull(message = "id cannot be empty") Integer userId) {
        return aclUserService.removeUserById(userId);
    }

    /**
     * 为用户分配角色
     * @param userId
     * @param roleId
     * @return
     */
    @PostMapping("/assignRole")
    @PreAuthorize("hasAuthority('user.assignRole')")
    public R assignRole(@RequestParam("userId") @NotNull(message = "userId is NULL") Integer userId,
                        @RequestParam("roleId") @NotNull(message = "roleId is NULL") Integer roleId) {
        return aclUserService.assignRole(userId, roleId);
    }
}
